package com.cci.kangdao.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cci.kangdao.service.AuthService;
import com.cci.kangdao.utilTool.ParameterTool;

@Controller
public class AuthRest {

	private final static Logger log = Logger.getLogger(AuthRest.class);
	
	@Autowired
	private AuthService authService;
	
	/**
	 * 获取用户组关联角色接口
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/getRolesList.aspx")
	public ModelAndView getRolesList(HttpServletRequest request,
			HttpServletResponse response) {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return authService.getRolesList(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * 扫码授权验证接口
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/authCheck.aspx")
	public ModelAndView authCheck(HttpServletRequest request,
			HttpServletResponse response) {
		try{
			if(request.getHeader("userid") == null  || request.getHeader("userid").equals("")){
				return new ModelAndView("/authCheck");
			} else {
				Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
				map.put("response", response);
				map.put("request", request);
				return authService.authCheck(map);
			}
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * 提交权限修改接口
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/authChange.aspx")
	public ModelAndView authChange(HttpServletRequest request,
			HttpServletResponse response) {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return authService.authChange(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
}
